package com.solomon.travel.common.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.solomon.travel.common.custom.mapper.UserCustomMapper;
import com.solomon.travel.common.mapper.UserMapper;
import com.solomon.travel.common.model.Page;
import com.solomon.travel.common.po.User;
import com.solomon.travel.common.po.UserExample;
import com.solomon.travel.common.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private UserCustomMapper userCustomMapper;

	@Override
	public int registe(User user) {
		return userMapper.insertSelective(user);
	}

	@Override
	public User login(String username, String password) {
		UserExample example = new UserExample();
		example.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
		List<User> users = userMapper.selectByExample(example);
		if (!users.isEmpty()) {
			return users.get(0);
		}
		return null;
	}

	@Override
	public User getByUsername(String username) {
		UserExample example = new UserExample();
		example.createCriteria().andUsernameEqualTo(username);
		List<User> users = userMapper.selectByExample(example);
		if (!users.isEmpty()) {
			return users.get(0);
		}
		return null;
	}

	@Override
	public User get(String id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public int count() {
		return userMapper.countByExample(null);
	}

	@Override
	public int countByOrgan(String organizeId) {
		UserExample example = new UserExample();
		example.createCriteria().andOrganizationIdEqualTo(organizeId);
		return userMapper.countByExample(example);
	}

	@Override
	public List<User> list(Page page) {
		return userCustomMapper.list(page);
	}

	@Override
	public List<User> listByOrgan(String organizeId, Page page) {
		return userCustomMapper.listByOrgan(organizeId, page);
	}

	@Override
	public int countByType(Integer type) {
		UserExample example = new UserExample();
		example.createCriteria().andTypeEqualTo(type);
		return userMapper.countByExample(example);
	}

	@Override
	public List<User> listByType(Integer type, Page page) {
		return userCustomMapper.listByType(type,page);
	}

	@Override
	public int update(User user) {
		return userMapper.updateByPrimaryKeySelective(user);
	}

}
